package org.elanfox.intellicommunity.intellicommunityadmin.controller.resident;

import org.elanfox.intellicommunity.biz.dto.NodeDto;
import org.elanfox.intellicommunity.biz.dto.condition.LocatorStructureCondition;
import org.elanfox.intellicommunity.biz.dto.condition.OrgCondition;
import org.elanfox.intellicommunity.biz.dto.condition.ResidentCondition;
import org.elanfox.intellicommunity.biz.dto.condition.WorkerCondition;
import org.elanfox.intellicommunity.biz.dto.resident.*;
import org.elanfox.intellicommunity.biz.entity.resident.*;
import org.elanfox.intellicommunity.biz.service.core.equipment.CardService;
import org.elanfox.intellicommunity.biz.service.core.resident.*;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.elanfox.intellicommunity.common.base.ResultCodeEnum;
import org.elanfox.intellicommunity.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author loutianxi
 * @date 2019-12-08
 */
@Controller
@RequestMapping("/resident")
public class ResidentController {
    @Autowired
    private ResidentService residentService;
    @Autowired
    private StructureService structureService;
    @Autowired
    private WorkerService workerService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private CardService cardService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private WorkerStructureService workerStructureService;
    @Autowired
    private LocatorStructureService locatorStructureService;

    /**
     * 跳转到住户列表页面
     *
     * @return
     */
    @RequestMapping("/list")
    public String residentList(@ModelAttribute("condition") ResidentCondition residentCondition,
                               @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        residentCondition.setDelFlag(true);
        Page<ResidentPO> page = residentService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, residentCondition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("residents", page.getContent());
        return "resident/resident_list";
    }

    /**
     * 新增/修改住户信息
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/ajax/save", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveResident(ResidentSaveDTO dto) {
        ResidentPO residentPO = residentService.save(dto);
        if (residentPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, dto);
    }

    /**
     * 跳转到部门列表页面
     *
     * @return
     */
    @RequestMapping("/org/list")
    public String orgList(@ModelAttribute("condition") OrgCondition orgCondition, @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        orgCondition.setDelFlag(true);
        Page<OrganizationPO> page = organizationService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, orgCondition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("orgList", page.getContent());
        return "resident/org_list";
    }

    /**
     * 新增/修改部门信息
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/save/org", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveOrg(OrganizationSaveDTO dto) {
        OrganizationPO organizationPO = organizationService.save(dto);
        if (organizationPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, dto);
    }

    /**
     * 跳转到工作人员列表页面
     *
     * @return
     */
    @RequestMapping("/worker/list")
    public String workerList(@ModelAttribute("condition") WorkerCondition workerCondition,
                             @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        workerCondition.setDelFlag(true);
        Page<WorkerPO> page = workerService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, workerCondition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("workers", page.getContent());
        return "resident/worker_list";
    }

    /**
     * 新增/修改部门信息
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "/save/worker", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveOrg(WorkerSaveDTO dto) {
        WorkerPO workerPO = workerService.save(dto);
        if (workerPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, dto);
    }

    /**
     * 卡解绑
     *
     * @param cardNo
     * @return
     */
    @RequestMapping(value = "/unbinding", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult unbinding(Long cardNo) {
        if (cardService.unbinding(cardNo)) {
            return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
        }
        return ApiResult.resultWith(ResultCodeEnum.ERROR);
    }

    /**
     * 跳转到社区树编辑页面
     *
     * @return
     */
    @RequestMapping("/structure/tree")
    public String structureTree() {
        return "resident/structure_tree";
    }

    /**
     * 获取社区数据
     *
     * @return
     */
    @RequestMapping(value = "/structure/tree/data", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult structureTreeData() {
        List<NodeDto> list = structureService.findTree();
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/structure", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteStructure(Integer id) {
        structureService.deleteStr(id);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "/save/structure", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveStructure(StructureSaveDTO dto) {
        StructurePO obj = structureService.save(dto);
        if (obj == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, obj);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delUser(Integer userId) {
        userService.delUser(userId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "/del/org", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delOrg(Integer id) {
        organizationService.del(id);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }


    /**
     * 跳转到房间定位页面
     *
     * @param model
     * @param strId
     * @return
     */
    @RequestMapping(value = {"/to/add/room/page"})
    public String addRoomPage(Model model, @RequestParam(defaultValue = "1") Integer strId) {
        NodeDto nodeDto = structureService.findNode(strId);
        model.addAttribute("data", nodeDto.getAllUrl());
        model.addAttribute("strId", strId);
        model.addAttribute("strName", nodeDto.getText());
        return "map/add_room";
    }

    /**
     * 根据楼层id获取该楼层的所有房间
     *
     * @param strId
     * @return
     */
    @RequestMapping(value = "/room/strId", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult roomList(Integer strId) {
        List<RoomPO> list = roomService.findByStrId(strId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    @RequestMapping(value = "/save/room", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveRoom(RoomSaveDTO dto) {
        RoomPO roomPO = roomService.save(dto);
        if (roomPO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED);
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, roomPO);
    }

    @RequestMapping(value = "/delete/room", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteRoom(Integer id) {
        roomService.delete(id);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 设置管家负责的楼层
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/save/rel", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult setPower(@RequestParam Integer relId,
                              @RequestParam String strIds) {
        workerStructureService.save(relId, strIds);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 回显管家负责的楼层
     *
     * @param relId
     * @return
     */
    @RequestMapping(value = "/rel/list", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult setPower(@RequestParam Integer relId) {
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, workerStructureService.findByWorkerId(relId));
    }

    /**
     * 跳转到附近定位器列表页面
     *
     * @return
     */
    @RequestMapping("/locator/structure/rel/list")
    public String locatorStructureRelList(@ModelAttribute("condition") LocatorStructureCondition orgCondition, @RequestParam(required = false, defaultValue = "1") Integer pageIndex, Model model) {
        Page<LocatorStructureRelPO> page = locatorStructureService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, orgCondition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", page.getTotalElements());
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("orgList", page.getContent());
        return "resident/locator_structure_rel_list";
    }

    /**
     * 删除附近定位器
     *
     * @param relId
     * @return
     */
    @RequestMapping(value = "/del/rel", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult delRel(@RequestParam Integer relId) {
        locatorStructureService.deleteById(relId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存附近定位器
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping(value = "/save/locator/rel", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveLocatorRel(@RequestParam Integer structureId,
                                    @RequestParam Long no) {
        locatorStructureService.save(structureId, no);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS);
    }
}
